from django.db import models
from numpy import *


class Turns(models.Model):

	idT = models.IntegerField(primary_key=True)
	idP = models.IntegerField()
	turn_type = models.IntegerField()
	date = models.DateField()
	doping_joola = models.IntegerField()
	doping_spric = models.IntegerField()
	field_fc_value = models.IntegerField()
	field_type = models.IntegerField()
	serv1_id = models.IntegerField()
	serv2_id = models.IntegerField()
	serv3_id = models.IntegerField()
	base_def_id = models.IntegerField()
	hit1_id = models.IntegerField()
	hit2_id = models.IntegerField()
	hit3_id = models.IntegerField()
	final_score = models.IntegerField()
	
	def __unicode__(self):
		return str(self.idP)+"-"+str(self.idT)

class Pandoleros(models.Model):

	idP = models.IntegerField(primary_key=True)
	name = models.CharField(max_length=100)
	default_joola = models.IntegerField()
	default_spric = models.IntegerField()
	num_turn_game = models.IntegerField()
	num_turn_trening = models.IntegerField()	
	num_mazze_game = models.IntegerField()
	num_mazze_trening = models.IntegerField()
	idU = models.IntegerField()

	t=[]
	ut=[]
	
	def getTurns(self,p=None):
		'''inicializiraj turne, ko je igralec igral/ni igral'''
		turns=Turns.objects.all()
		if p == None:
			self.t=[turn for turn in turns if turn.idP == self.idP]
			self.ut=[turn for turn in turns if not turn.idP == self.idP]
	
		elif p == 'trening':
			self.t=[turn for turn in turns if turn.idP == self.idP and turn.turn_type == 1]
			self.ut=[turn for turn in turns if not turn.idP == self.idP and turn.turn_type == 1]
			
		elif p == 'tekma':
			self.t=[turn for turn in turns if turn.idP == self.idP and turn.turn_type == 0] 
			self.ut=[turn for turn in turns if not turn.idP == self.idP and turn.turn_type == 0]
			
			
	def __unicode__(self):
		return self.name

#######
#NAPAD#
#######

	def scoreVector(self):
		s =  [turn.final_score for turn in self.t]
		return s
	
	def scoreAverage(self):
		v=self.scoreVector()
		return float(sum(v))/len(v)
	
	def scoreTotal(self):
		v=self.scoreVector()
		return sum(v)
	def fcVector(self):
		fc = [[],[],[]]
		for turn in self.t:
			s = (not turn.hit1_id)+(not turn.hit2_id)+(not turn.hit3_id)
				

			if s==1:
				fc[0].append(turn.field_fc_value)
				fc[1].append(0)
				fc[2].append(0)
				
			elif s==2:
				fc[0].append(0)
				fc[1].append(turn.field_fc_value*2)
				fc[2].append(0)
				
			elif s==3:
				fc[0].append(0)
				fc[1].append(0)
				fc[2].append(turn.field_fc_value*3)
			
			else:
				fc[0].append(0)
				fc[1].append(0)
				fc[2].append(0)
			
		return fc
	
	def fcTotal(self):
		count = 0
		for turn in self.t:
			count += (not turn.hit1_id) + (not turn.hit2_id) + (not turn.hit3_id)
		return count
	def fcAverage(self):
		return self.fcTotal()/float(len(self.t))
	
	def ccoef(self,a,b):
		'''Korelacijski koeficient'''
		from numpy import std
		from numpy import mean
		ma = mean(a)
		mb = mean(b)
		cov = 0
		for i in range (0, len(a)):
			cov = cov + (a[i]-ma)*(b[i]-mb)
			

		return cov/(std(a)*std(b)*len(a))
	
	def dopingCC(self, g=None):
		c = 0;
		scores=[0]
		dopings=[0]
		if g == 'both':
			dopings = [i.doping_spric+i.doping_joola for i in self.t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in self.t if i.doping_spric+i.doping_joola==d]
				scores.append(average(sc))

		elif g == 'joola':
			dopings = [i.doping_joola for i in self.t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in self.t if i.doping_joola==d]
				scores.append(average(sc))

			

		elif g == 'spric':
			dopings = [i.doping_spric for i in self.t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in self.t if i.doping_spric==d]
				scores.append(average(sc))

		return self.ccoef(dopings,scores)


	def dopingVector(self,joola,spric):
		
		x=range(6)
		if(joola and spric):
			x=range(11)
		y=[]

		for j in x:
			overall = joola*spric;
			v = [k.final_score for k in self.t if (joola and k.doping_joola==j) or (spric and k.doping_spric==j) or (overall and (k.doping_joola + k.doping_spric == j))]
			y.append(sum(v)/float(len(v)))
			
		return [x,y]

	def servDistributionVector(self):
		servVector = [0,0,0]
		for turn in self.t:
			servVector[0] += not(turn.serv1_id)
			servVector[1] += not(turn.serv2_id)
			servVector[2] += not(turn.serv3_id)

			
		s=float(sum(servVector))
		servVector[0] = servVector[0]/s
		servVector[1] = servVector[1]/s
		servVector[2] = servVector[2]/s

		return servVector
	#TESTIRAJ
	def servSucess(self):
		serv = 0
		count = 0
		for turn in self.t:
			if turn.serv1_id >= -2:
				serv=serv+(not turn.serv1_id)
				count = count+1

			if turn.serv2_id >= -2:
				serv=serv+(not turn.serv2_id)
				count = count+1

			if turn.serv3_id >= -2:
				serv=serv+(not turn.serv3_id)
				count = count+1

		return float(serv)/count
	def servTurn(self):
		serv = 0
		count = 0
		for turn in self.t:
			serv += (turn.serv1_id ==0 or turn.serv2_id ==0 or turn.serv3_id ==0)
			count+=1;		
		
		return float(serv)/count
	def servReasonVector(self):
		servVector = [0,0,0]
		for turn in self.t:
			s=[turn.serv1_id, turn.serv2_id, turn.serv3_id]
			for serv in s:
				if serv > -1024 and not serv == 0:
					servVector[0] += (serv == -1)
					servVector[1] += (serv == -2)
					servVector[2] += (serv > 0)
			
		unCount=sum(servVector);
		return [float(i)/unCount for i in servVector]
		

		
	
	def hitPositive(self):
		hit = 0
		count = 0
		for turn in self.t:
			hit += (turn.final_score >0)
			count+=1;		
		
		return float(hit)/count

	
	
	def hitEventVector(self):
		hitVector=[0,0,0,0,0,0]
		for turn in self.t:
			s=[turn.hit1_id, turn.hit2_id, turn.hit3_id]
			for hit in s:
					hitVector[0] += (hit == 0)
					hitVector[1] += (hit == -1)
					hitVector[2] += (hit == -2)
					hitVector[3] += (hit == -3)
					hitVector[4] += (hit == -4)
					hitVector[5] += (hit > 0)
			
		unCount=sum(hitVector);
		return [float(i)/unCount for i in hitVector]
	
	
	def hitReasonVector(self):
		'''[zgresil pandolo, out, ulovljen]'''
		hitVector = [0,0,0]


		for turn in self.t:
			s=[turn.hit1_id, turn.hit2_id, turn.hit3_id]
			for hit in s:
				if hit > -1024 and not hit == 0 and not hit == -1 and not hit == -2:
					hitVector[0] += (hit == -4)
					hitVector[1] += (hit == -3)
					hitVector[2] += (hit > 0)
			
		unCount=sum(hitVector);
		return [float(i)/unCount for i in hitVector]
	
	
	
	

	
	#--DEPRECATED
	def average_score(self, p=None):
		

		if p == None:
			return (self.num_mazze_game+self.num_mazze_trening)/float(self.num_turn_trening+self.num_turn_game);
	
		elif p == 'trening':
			a_t = self.num_mazze_trening/float(self.num_turn_trening)
			return a_t;

		elif p == 'tekma':
			a_g = self.num_mazze_game/float(self.num_turn_game)
			return a_g;

		else:
			pass
	


	#
	def fcCount(self, p=None):
		
		
		t=Turns.objects.filter(idP=self.idP)
				
		if p == 'trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		if p == 'tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		i = 0
		for j in t:
			if j.hit1_id == 0:
				i=i+1;
			if j.hit2_id == 0:
				i=i+1;
			if j.hit3_id == 0:
				i=i+1;
		return i

	
	#
	def fcRatio(self, p=None, g='whole'):
	

		i=2
		if g == 'turn':
			i=1

		if p == None:
			t=Turns.objects.filter(idP=self.idP)
			return i*self.fcCount(None)/float(len(t));
				
		elif p == 'trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)
			return i*self.fcCount('trening')/float(len(t));

		elif p == 'tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)
			return i*self.fcCount('game')/float(len(t));


	#
	def totalScoreCount(self, p=None):
	
		if p == None:
			t=Turns.objects.filter(idP=self.idP)
				
		elif p == 'trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		elif p == 'tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		i = 0
		for j in t:
			i=i+j.final_score

		return i

	#
	def dopingCorCoef(self,p=None,g='both'):
	

		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)
	
		c = 0;
		scores=[0]
		dopings=[0]
		if g == 'both':
			dopings = [i.doping_spric+i.doping_joola for i in t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in t if i.doping_spric+i.doping_joola==d]
				scores.append(average(sc))

		elif g == 'joola':
			dopings = [i.doping_joola for i in t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in t if i.doping_joola==d]
				scores.append(average(sc))

			

		elif g == 'spric':
			dopings = [i.doping_spric for i in t]

			scores=[]
			for d in dopings:
				sc = [i.final_score for i in t if i.doping_spric==d]
				scores.append(average(sc))

		return self.ccoef(dopings,scores)



	#
	def servPercentage(self, p=None):
	
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		servVector = [0,0,0]
		for turn in t:
			servVector[0] += not(turn.serv1_id)
			servVector[1] += not(turn.serv2_id)
			servVector[2] += not(turn.serv3_id)

			
		s=float(sum(servVector))
		servVector[0] = servVector[0]/s
		servVector[1] = servVector[1]/s
		servVector[2] = servVector[2]/s

		return servVector

	#
	def sucessfulServ(self, p=None):
	
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		serv = 0
		count = 0
		for turn in t:
			if turn.serv1_id >= -2:
				serv=serv+(not turn.serv1_id)
				count = count+1

			if turn.serv2_id >= -2:
				serv=serv+(not turn.serv2_id)
				count = count+1

			if turn.serv3_id >= -2:
				serv=serv+(not turn.serv3_id)
				count = count+1

		return float(serv)/count


	
	#
	def turnServ(self, p=None):
	
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		serv = 0
		count = 0
		for turn in t:
			serv += (turn.serv1_id ==0 or turn.serv2_id ==0 or turn.serv3_id ==0)
			count+=1;		
		
		return float(serv)/count
	
	#
	def turnHit(self, p=None):

		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		hit = 0
		count = 0
		for turn in t:
			hit += (turn.final_score >0)
			count+=1;		
		
		return float(hit)/count


	
	#
	def unServReason(self, p=None):
	
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		#[zgresil pandolo, out, ulovljen]#
		servVector = [0,0,0]


		for turn in t:
			s=[turn.serv1_id, turn.serv2_id, turn.serv3_id]
			for serv in s:
				if serv > -1024 and not serv == 0:
					servVector[0] += (serv == -1)
					servVector[1] += (serv == -2)
					servVector[2] += (serv > 0)
			
		unCount=sum(servVector);
		return [float(i)/unCount for i in servVector]
	
	
	
	#
	def hitEventVectorD(self, p=None):
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		#[fouri campo, +12-fc, -12, zgresil pandolo, out, ulovljen]#
		hitVector = [0,0,0,0,0,0]

		for turn in t:
			s=[turn.hit1_id, turn.hit2_id, turn.hit3_id]
			for hit in s:
					hitVector[0] += (hit == 0)
					hitVector[1] += (hit == -1)
					hitVector[2] += (hit == -2)
					hitVector[3] += (hit == -3)
					hitVector[4] += (hit == -4)
					hitVector[5] += (hit > 0)
			
		unCount=sum(hitVector);
		return [float(i)/unCount for i in hitVector]
	
	
	#
	def unHitReason(self, p=None):
		if p==None:
			t=Turns.objects.filter(idP=self.idP)
	
		elif p=='tekma':
			t=Turns.objects.filter(idP=self.idP, turn_type=0)

		elif p=='trening':
			t=Turns.objects.filter(idP=self.idP, turn_type=1)

		else:
			pass

		#[zgresil pandolo, out, ulovljen]#
		hitVector = [0,0,0]


		for turn in t:
			s=[turn.hit1_id, turn.hit2_id, turn.hit3_id]
			for hit in s:
				if hit > -1024 and not hit == 0 and not hit == -1 and not hit == -2:
					hitVector[0] += (hit == -3)
					hitVector[1] += (hit == -4)
					hitVector[2] += (hit > 0)
			
		unCount=sum(hitVector);
		return [float(i)/unCount for i in hitVector]



#########
#OBRAMBA#
#########

	def caughtPanCount(self):
		'''ulovljeni pandoli'''

		i = 0
		for j in self.ut:
			i=i+(j.serv1_id==self.idP);
			i=i+(j.serv2_id==self.idP);
			i=i+(j.serv3_id==self.idP);
			i=i+(j.hit1_id==self.idP);
			i=i+(j.hit2_id==self.idP);
			i=i+(j.hit3_id==self.idP);

		return i

	def caughtPanCountVector(self):
		'''pandole je ulovil'''
		x = []
		y = []
		x=[p.idP for p in Pandoleros.objects.all()]
		
		for j in x:
			i = 0.001
			if j == self.idP:
				y.append(0.001)
				continue
			for k in self.ut:
				i = i+ (k.idP == j and k.hit1_id == self.idP)
				i = i+ (k.idP == j and k.hit2_id == self.idP)
				i = i+ (k.idP == j and k.hit3_id == self.idP)
				i = i+ (k.idP == j and k.serv1_id == self.idP)
				i = i+ (k.idP == j and k.serv2_id == self.idP)
				i = i+ (k.idP == j and k.serv3_id == self.idP)
			y.append(i)
		
		return [x,y]

	def caughtPanCountOthers(self):
		'''ulovljeni pandoli od drugih oboje/trening/tekma'''
		i = 0
		for j in self.t:
			i=i+(j.serv1_id>0);
			i=i+(j.serv2_id>0);
			i=i+(j.serv3_id>0);
			i=i+(j.hit1_id>0);
			i=i+(j.hit2_id>0);
			i=i+(j.hit3_id>0);

		return i
	
	def caughtPanCountOthersVector(self):
		'''pandole so mu ulovili'''
		x = []
		y = []
		x=[p.idP for p in Pandoleros.objects.all()]
		
		for j in x:
			i = 0.001
			if j == self.idP:
				y.append(0.001)
				continue
			for k in self.t:
				i = i+ (k.hit1_id == j)
				i = i+ (k.hit2_id == j)
				i = i+ (k.hit3_id == j)
				i = i+ (k.serv1_id == j)
				i = i+ (k.serv2_id == j)
				i = i+ (k.serv3_id == j)
			y.append(i)
		
		return [x,y]
	
	
	
	def baseHitCountOthers(self):
		'''izbite baze'''
		i = 0
		for j in self.t:
			i=i+(j.base_def_id>0);
		
		return i

	
	def baseHitCount(self):
		'''zadete baze'''
		i = 0
		for j in self.ut:
			i=i+(j.base_def_id==self.idP);
		
		return i


	def baseHitCountVector(self):
		'''bazo je izbil'''
		x = []
		y = []
		x=[p.idP for p in Pandoleros.objects.all()]
		
		for j in x:
			i = 0.001
			if j == self.idP:
				y.append(0.001)
				continue
			for k in self.ut:
				i = i+ (k.idP == j and k.base_def_id == self.idP)
			y.append(i)
		
		return [x,y]
	
	
	


	def baseHitCountOthersVector(self):
		'''bazo so mu izbili'''
		x = []
		y = []
		x=[p.idP for p in Pandoleros.objects.all()]
		
		for j in x:
			i = 0.001
			if j == self.idP:
				y.append(0.001)
				continue
			for k in self.t:
				i = i+ (k.base_def_id == j)
			y.append(i)
		
		return [x,y]